package com.dgut.edu.cn.design.mode.strategy.controller;


import com.dgut.edu.cn.design.mode.strategy.StrategyServiceManager;
import com.dgut.edu.cn.design.mode.strategy.StrategyType;
import com.dgut.edu.cn.design.mode.strategy.compare.PriceCompareStrategy;
import com.dgut.edu.cn.design.mode.strategy.control.PriceControlStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: hongbin.zheng
 * @create: 2023-07-18 10:05
 **/
@RestController
@RequestMapping("/strategy")
public class StrategyController {

	@Autowired
	private StrategyServiceManager strategyServiceManager;

	@RequestMapping("/test")
	public String helloTest() {
		return "test";
	}

	@RequestMapping("/test2")
	public String test2(@RequestParam("strategyName") String strategyName, @RequestParam("dataType") String dataType) {
		// 获取到的strategyName值为"1"，获取到的dataType值为"2"
		return strategyName + " " + dataType;
	}


	@RequestMapping("/testPriceCompare")
	public Object testPriceCompare(@RequestParam("strategyName") String strategyName
			, @RequestParam("dataType") String dataType) {
		StrategyType strategyType = StrategyType.valueOf(strategyName);
		if (strategyType == null) {
			return "策略不存在";
		}
		PriceCompareStrategy strategyInstance = strategyServiceManager
				.getStrategyInstance(strategyType, dataType, PriceCompareStrategy.class);
		if (strategyInstance == null) {
			return "找不到比价策略";
		}

		return strategyInstance.doPriceCompare();
	}

	@RequestMapping("/testPriceControl")
	public Object testPriceControl(@RequestParam(defaultValue = "strategyName") String strategyName
			, @RequestParam(defaultValue = "dataType") String dataType) {
		StrategyType strategyType = StrategyType.valueOf(strategyName);
		if (strategyType == null) {
			return "管控策略不存在";
		}
		PriceControlStrategy strategyInstance = strategyServiceManager
				.getStrategyInstance(strategyType, dataType, PriceControlStrategy.class);
		if (strategyInstance == null) {
			return "找不到管控策略";
		}

		return strategyInstance.doPriceControl();
	}
}
